package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class MainActivity extends AppCompatActivity {

    ListView listView;
    ArrayList<Person> list = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = findViewById(R.id.lv);
        addtion();
        listView.setAdapter(new MyAdapter());
    }
    public void addtion(){
        Person p1 = new Person("zhangsan", 18);
        Person p2 = new Person("lisi", 19);
        Person p3 = new Person("wangwu", 20);
        Collections.addAll(list, p1, p2, p3);
    }
    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ont, parent, false);
            TextView name = view.findViewById(R.id.tv_one);
            TextView age = view.findViewById(R.id.tv_two);
            name.setText(list.get(position).getName());
            age.setText(list.get(position).getAge()+"");
            return view;
        }
    }
}